package com.xique.door.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.utils.poi.ExcelUtil;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.door.bean.domain.fenqiao.Community;
import com.xique.door.bean.request.fenqiao.CommunityPersonInfoRequest;
import com.xique.door.constant.FenQiaoConstant;
import com.xique.door.service.CommunityService;
import com.xique.door.util.QRCodeUtils;
import com.xique.park.mapper.CommunityMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import javax.swing.text.html.Option;
import java.io.IOException;
import java.util.*;

/**
 *
 */
@Service
public class CommunityServiceImpl extends ServiceImpl<CommunityMapper, Community>
    implements CommunityService {




    @Override
    @Transactional
    public AjaxResult addPersonInfo(List<Community> communitys) {

        //业主
         long finalParentId = 0L;
        ArrayList<Community> list = new ArrayList<>();

        for (int i = 0; i < communitys.size(); i++) {
            Community community = communitys.get(i);
            community.setCtreateTime(new Date());
            if (community.getIsOwner() == FenQiaoConstant.OWNER){
                this.save(community);
                communitys.remove(i);
                finalParentId = community.getId();
            }
        }

        long ParentId = finalParentId;
        communitys.forEach(community -> {community.setPid(ParentId);});


        list.addAll(communitys);

       if (CollectionUtils.isNotEmpty(communitys)){
           boolean flag = this.saveBatch(communitys);
           if (flag== false){
               return AjaxResult.success("人员新增信息采集失败");
           }
       }

       return AjaxResult.success("人员新增信息采集成功");
    }


    @Override
    public AjaxResult selectPersonInfo(CommunityPersonInfoRequest request) {

        LambdaQueryWrapper<Community> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(request.getType()!=null,Community::getType,request.getType());
        queryWrapper.eq(StringUtils.isNotBlank(request.getOwnerName()),Community::getOwnerName,request.getOwnerName());
        queryWrapper.eq(StringUtils.isNotBlank(request.getOwnerPhone()),Community::getOwnerName,request.getOwnerName());
        queryWrapper.eq(StringUtils.isNotBlank(request.getCommunityName()),Community::getOwnerName,request.getOwnerName());
        queryWrapper.eq(StringUtils.isNotBlank(request.getCommunityAddress()),Community::getOwnerName,request.getOwnerName());
        queryWrapper.eq(request.getOwnerSex()!=null,Community::getOwnerSex,request.getOwnerSex());


        Page<Community> page = new Page<Community>();
        page.setCurrent(request.getPageNum());
        page.setSize(request.getPageSize());

        Page<Community> result = this.page(page, queryWrapper);

        HashMap<String, Object> map = new HashMap<>(3);
        map.put("list",result.getRecords());
        map.put("count",result.getTotal());
        map.put("pages",result.getPages());
        return AjaxResult.success(map);
    }


    @Override
    public AjaxResult deletePersonInfo(Long[] ids) {

         List<Long> longs = Arrays.asList(ids);

         boolean flag = this.removeByIds(longs);

        if (flag){
            return AjaxResult.success("删除人员成功!");
        }

        return AjaxResult.error("删除人员失败!");

    }


    @Override
    public AjaxResult updatePersonInfo(List<Community> communitys) {


        boolean flag = this.updateBatchById(communitys);


        if (flag == true){
            return AjaxResult.success("更新人员信息成功!");
        }
        return AjaxResult.error("更新人员信息失败!");
    }


    @Override
    public void export(HttpServletResponse response, CommunityPersonInfoRequest request) throws IOException {

        LambdaQueryWrapper<Community> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(request.getType()!=null,Community::getType,request.getType());
        queryWrapper.eq(StringUtils.isNotBlank(request.getOwnerName()),Community::getOwnerName,request.getOwnerName());
        queryWrapper.eq(StringUtils.isNotBlank(request.getOwnerPhone()),Community::getOwnerName,request.getOwnerName());
        queryWrapper.eq(StringUtils.isNotBlank(request.getCommunityName()),Community::getOwnerName,request.getOwnerName());
        queryWrapper.eq(StringUtils.isNotBlank(request.getCommunityAddress()),Community::getOwnerName,request.getOwnerName());
        queryWrapper.eq(request.getOwnerSex()!=null,Community::getOwnerSex,request.getOwnerSex());

        List<Community> list = this.list(queryWrapper);

        ExcelUtil<Community> util = new ExcelUtil<Community>(Community.class);
        util.exportExcel(response, list, "社区人员采集信息导出");
    }


    @Override
    public AjaxResult selectDetail(Long id) {

        LambdaQueryWrapper<Community> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Community::getId,id);
        List<Community> list = new ArrayList<>();
        Community community = this.getOne(queryWrapper);
        if (community == null){
            return AjaxResult.error("没有当前相关联用户");
        }
        if (community.getIsOwner() == FenQiaoConstant.OWNER){
            list.add(community);
            List<Community> communities = this.list(new LambdaQueryWrapper<Community>().eq(Community::getPid, community.getId()));
            if (CollectionUtils.isNotEmpty(communities)){
                list.addAll(communities);
            }
        }else {
            Community owner = this.getOne(new LambdaQueryWrapper<Community>().eq(Community::getPid, id));
            list.add(owner);
            List<Community> communities = this.list(new LambdaQueryWrapper<Community>().eq(Community::getPid, owner.getId()));
            list.addAll(communities);
        }
        return AjaxResult.success(list);

    }
}




